package com.midea.ai.air.ui.more.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseFragment;
import com.midea.ai.air.ui.more.adapter.MyFamilyAdapter;
import com.midea.ai.air.ui.more.data.MyFamily;
import com.midea.ai.air.view.AlertCenterDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ouyangdi on 2016/4/16.
 * 更多->我的家庭->选择家庭
 */
public class MoreFamilySelectFragment extends BaseFragment {
    private TextView mTvMyFamily;
    private ListView mListFamiliesJoin;
    private List<MyFamily> mMyFamilies;
    private MyFamilyAdapter mFamilyAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_more_family_select_family, container, false);
        initData();
        initView(view);
        return view;
    }

    private void initData() {
        mMyFamilies = new ArrayList<>();
        mMyFamilies.add(new MyFamily(R.drawable.air_more_family_photo1, "zhangsan的家", false));
        mMyFamilies.add(new MyFamily(R.drawable.air_more_family_photo2, "lisi的家", false));
        mFamilyAdapter = new MyFamilyAdapter(getActivity(), 0, mMyFamilies);
    }

    private void initView(View view) {
        mTvMyFamily = (TextView) view.findViewById(R.id.txtView_family_name);
        mTvMyFamily.setText("展厅");

        mListFamiliesJoin = (ListView)view.findViewById(R.id.list_family_join);
        mListFamiliesJoin.setAdapter(mFamilyAdapter);
        mListFamiliesJoin.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MyFamily item = (MyFamily) parent.getAdapter().getItem(position);
                if(!item.isSelected()) {
                    final int index = position;
                    AlertCenterDialog dialog = new AlertCenterDialog(getActivity()).builder().setTitle(getString(R.string.air_more_family_change))
                            .setMsg(getString(R.string.air_more_family_change_msg)+item.getStrName() + getString(R.string.air_more_family_change_msg_end))
                            .setPositiveButton("", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mMyFamilies.get(index).setSelected(true);
                                    for(int i=0; i<mMyFamilies.size(); i++) {
                                        if(i!=index) {
                                            mMyFamilies.get(i).setSelected(false);
                                        }
                                    }
                                    mFamilyAdapter.notifyDataSetChanged();
                                }
                            })
                            .setNegativeButton("", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                }
                            });
                    dialog.show();
                }
            }
        });
    }
}
